package LeetCode_Stack;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:逆波兰表达式求值
 * User: 阿宾
 * Date: 2024-11-14
 * Time: 08:59
 */
public class Demo2 {
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i< tokens.length;i++){
            if(!isOperator(tokens[i])){//不是操作符，将数据压栈
                //将字符串转为整型类型：使用Integer.valueof(字符串)
                stack.push(Integer.valueOf(tokens[i]));
            }else {
                //是操作符，弹栈进行计算
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (tokens[i]){
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;

                }
            }


        }
        return stack.peek();
    }
    public static boolean isOperator(String s){
        if(s.equals("+") ||s.equals("-") ||s.equals("*") ||s.equals("/")){
            return true;
        }
        return false;
    }
    public static void main(String[] args) {

    }
}
